package com.zsh.controller;

import com.zsh.config.ProductClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Administrator on 2018/6/10 0010.
 */
@RestController
public class ConsumerTest {


    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProductClient productClient;

    @RequestMapping("/getProduct")
    public String getProduct() {
       /*第一种方法*/
       /* RestTemplate restTemplate = new RestTemplate();
        String res = restTemplate.getForObject("http://localhost:8080/getName", String.class);*/
        /*第二种方法*/
        /*RestTemplate restTemplate = new RestTemplate();
        ServiceInstance serviceInstance= loadBalancerClient.choose("product");
        String url= String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort())+"/getName";
        String res = restTemplate.getForObject(url, String.class);*/

        /*第三种方法*/
        /*先获取到 restTemplate的实例*/
        String res = restTemplate.getForObject("http://product/getName", String.class);
        return res;
    }

    @RequestMapping("/getProductName")
    public String getProductNmae() {
        String res = productClient.getName();
        return res;
    }
}
